<%@ page import="com.qiku.pojo.User" %>
<%@ page import="java.util.Map" %><%--
  Created by IntelliJ IDEA.
  User: nnxy
  Date: 2022/6/8
  Time: 17:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户中心</title>
    <style>
        #father {
            width: 100%;
        }

        #title {
            min-width: 1600px;
            min-height: 50px;
            margin: 0 auto;
            background: #8365bdba;
        }

        #wenjian {
            width: 100%;
        }

        #content {
            width: 1600px;
            height: 850px;
            margin: 0 auto;
            background: skyblue;
        }

        #yonghu {
            display: block;
            text-align: center;
            font-size: 30px;
            font-weight: bold;
        }

        #tuichu{
            margin-left: 770px;
        }

        .Upload {
            width: 1300px;
            margin: 0 auto;
            border: 3px solid #d4ffd4;
        }

        .pane {
            text-align: center;
            font-size: 20px;
            font-weight: bold;
        }

        #t1, #t2 {
            margin: 0 auto;
            text-align: center;
        }

        #a {
            position: absolute;
            top: 20px;
            left: 20px;
        }

        table {
            background-color: rgb(233, 255, 194);
            border: 2px solid rgb(124 209 255);
            color: rgb(195, 82, 82);
            border-radius: 5px;
            font-size: 20px;
        }

        tr {
            background-color: rgba(203, 203, 203, 0.466);
        }

        th {
            background-color: rgb(255, 202, 174);
            border: 2px solid rgb(255, 170, 124);
            color: rgb(82, 106, 195);
            border-radius: 5px;
        }

        td {
            background-color: rgb(255, 202, 174);
            border: 2px solid rgb(255, 170, 124);
            border-radius: 0px;
        }

        a {
            text-decoration: none;
            color: greenyellow;
        }
    </style>
</head>
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<%
    String username = (String) session.getAttribute("username");
    User user = (User) session.getAttribute("user");
//    System.out.println("username:"+username);
    Map<String, String> userLoginMap = (Map<String, String>) application.getAttribute("userLoginMap");
    String sid = userLoginMap.get(username);
//    System.out.println("usernameSID:"+sid);
    String newSid = session.getId();
//    System.out.println("usernameNewSID:"+newSid);
    boolean isLogin = username != null && newSid.equals(sid);
    if (!isLogin) {
        response.sendRedirect("login.jsp");
    } else {
%>
<body onload="findResourceListByUserId();findUserName()">
<div id="father">
    <div id="title">
        <a href="main.jsp" id="a">返回主页</a>
        <span id="yonghu">用户: <%= user.getUsername()%></span>
        <input id="tuichu" type="button" value="退出登录" onclick="loginOut()">
    </div>
</div>


<div id="wenjian">
    <div id="content">
        <div class="Upload">
            <div class="pane">
                您上传的文件:
                <table border="1" id="t1">
                    <tr>
                        <th>文件名</th>
                        <th>文件类型</th>
                        <th>上传时间</th>
                    </tr>
                    <tr id="ffu0">
                        <td>
                            <a id="sffu0" class="rName" href=""></a>
                        </td>
                        <td class="type1">-</td>
                        <td class="upload_date">-</td>
                    </tr>
                    <tr id="ffu1">
                        <td>
                            <a id="sffu1" class="rName" href=""></a>
                        </td>
                        <td class="type1">-</td>
                        <td class="upload_date">-</td>
                    </tr>
                    <tr id="ffu2">
                        <td>
                            <a id="sffu2" class="rName" href=""></a>
                        </td>
                        <td class="type1">-</td>
                        <td class="upload_date">-</td>
                    </tr>
                    <tr id="ffu3">
                        <td>
                            <a id="sffu3" class="rName" href=""></a>
                        </td>
                        <td class="type1">-</td>
                        <td class="upload_date">-</td>
                    </tr>
                    <tr id="ffu4">
                        <td>
                            <a id="sffu4" class="rName" href=""></a>
                        </td>
                        <td class="type1">-</td>
                        <td class="upload_date">-</td>
                    </tr>
                    <tr id="ffu5">
                        <td>
                            <a id="sffu5" class="rName" href=""></a>
                        </td>
                        <td class="type1">-</td>
                        <td class="upload_date">-</td>
                    </tr>
                </table>
                <button id="shang1" onclick="fanye1(-1)">上一页</button>
                <span id="dangqianye1">1</span>
                /<span id="zongye1">1</span>
                <button id="xia1" onclick="fanye1(+1)">下一页</button>
            </div>
        </div>
        <br>
        <br>
        <div class="Upload">
            <div class="pane">
                您收藏的文件:
                <table border="1" id="t2">
                    <tr>
                        <th>文件名</th>
                        <th>文件类型</th>
                        <th>上传时间</th>
                        <th>点击取消</th>
                    </tr>
                    <tr id="fu0">
                        <td>
                            <input id="sfu0" class="ziyuanID" type='text' value="" hidden>
                            <a class="rNameUrl" href="" style=""></a>
                        </td>
                        <td class="type"></td>
                        <td class="uploadDate"></td>
                        <td><input class="quxiao" type="button" onclick="subCollect(this)" value="取消收藏"></td>
                    </tr>
                    <tr id="fu1">
                        <td>
                            <input id="sfu1" class="ziyuanID" type='text' value="" hidden>
                            <a class="rNameUrl" href="" style=""></a>
                        </td>
                        <td class="type"></td>
                        <td class="uploadDate"></td>
                        <td><input class="quxiao" type="button" onclick="subCollect(this)" value="取消收藏" hidden></td>
                    </tr>
                    <tr id="fu2">
                        <td>
                            <input id="sfu2" class="ziyuanID" type='text' value="" hidden>
                            <a class="rNameUrl" href="" style=""></a>
                        </td>
                        <td class="type"></td>
                        <td class="uploadDate"></td>
                        <td><input class="quxiao" type="button" onclick="subCollect(this)" value="取消收藏" hidden></td>
                    </tr>
                    <tr id="fu3">
                        <td>
                            <input id="sfu3" class="ziyuanID" type='text' value="" hidden>
                            <a class="rNameUrl" href="" style=""></a>
                        </td>
                        <td class="type"></td>
                        <td class="uploadDate"></td>
                        <td><input class="quxiao" type="button" onclick="subCollect(this)" value="取消收藏" hidden></td>
                    </tr>
                    <tr id="fu4">
                        <td>
                            <input id="sfu4" class="ziyuanID" type='text' value="" hidden>
                            <a class="rNameUrl" href="" style=""></a>
                        </td>
                        <td class="type"></td>
                        <td class="uploadDate"></td>
                        <td><input class="quxiao" type="button" onclick="subCollect(this)" value="取消收藏" hidden></td>
                    </tr>
                    <tr id="fu5">
                        <td>
                            <input id="sfu5" class="ziyuanID" type='text' value="" hidden>
                            <a class="rNameUrl" href="" style=""></a>
                        </td>
                        <td class="type"></td>
                        <td class="uploadDate"></td>
                        <td><input class="quxiao" type="button" onclick="subCollect(this)" value="取消收藏" hidden></td>
                    </tr>
                </table>
                <button id="shang" onclick="fanye(-1)">上一页</button>
                <span id="dangqianye">1</span>
                /<span id="zongye">1</span>
                <button id="xia" onclick="fanye(+1)">下一页</button>
            </div>
        </div>
    </div>
</div>
<%
    }
%>
</body>

<script>
    //退出登录
    function loginOut() {
        $.ajax({
            url: "loginOut",
            data: {},
            type: "get",
            dataType: "json",
            success: function (data) {
                alert(data.loginOutMsg);
                self.location = 'http://localhost:8080/ResourceBundleSite/login.jsp';
            },
            error: function () {
                alert("======错误======")
            }
        });
    }

    //实现翻页的函数
    function fanye(num) {
        //当前页
        var ye = parseInt($("#dangqianye").text());
        //总页
        var zongye = parseInt($("#zongye").text());
        switch (num) {
            case 1:
                if (ye < zongye) {
                    ye = ye + num;
                }
                break;
            case -1:
                if (ye > 1) {
                    ye = ye + num;
                }
                break;
        }
        $("#dangqianye").text(ye);
        findResourceListByUserId();
    }

    //实现翻页的函数1
    function fanye1(num) {
        //当前页
        var ye1 = parseInt($("#dangqianye1").text());
        //总页
        var zongye1 = parseInt($("#zongye1").text());
        switch (num) {
            case 1:
                if (ye1 < zongye1) {
                    ye1 = ye1 + num;
                }
                break;
            case -1:
                if (ye1 > 1) {
                    ye1 = ye1 + num;
                }
                break;
        }
        $("#dangqianye1").text(ye1);
        findUserName();
    }

    //取消收藏函数
    function subCollect(b) {
        let node = b.parentNode.parentNode;
        var nodeId = node.getAttribute("id");
        console.log("父类id：" + nodeId);
        var sfuId = "s" + nodeId;
        console.log("S父类id：" + sfuId);
        var rId = $("#" + sfuId).text();
        console.log("rID：" + rId);
        $.ajax({
            url: "collect",
            data: {
                "methodName": "subCollect",
                "resource_id": rId
            },
            type: "get",
            dataType: "json",
            success: function (data) {
                alert(data.subMsg)
                findResourceListByUserId();
            },
            error: function () {
                alert("======错误======")
            }
        });
    }

    //查询用户收藏列表
    function findResourceListByUserId() {
        var user_id =  <%= user.getId()%>;
        console.log(user_id);
        $.ajax({
            url: "collect",
            data: {
                "methodName": "findResourceListByUserId",
                "user_id": user_id
            },
            type: "get",
            dataType: "json",
            success: function (data) {
                //数据初始化
                var $ziyuanID = $(".ziyuanID");
                var $rNameUrl = $(".rNameUrl");
                var $type = $(".type");
                var $uploadDate = $(".uploadDate");
                var $quxiao = $(".quxiao");
                for (var i = 0; i < 6; i++) {
                    $ziyuanID.eq(i).text("");
                    $rNameUrl.eq(i).attr("href", "");
                    $rNameUrl.eq(i).attr("style", "");
                    $rNameUrl.eq(i).text("-");
                    $type.eq(i).text("-");
                    $uploadDate.eq(i).text("-");
                    $quxiao.eq(i).attr("hidden", true);
                    $quxiao.eq(i).val("-");
                }
                var ye = $("#dangqianye").text();
                var dangqian = parseInt(ye);
                var length = data.length;
                var yeshu = (length % 6 == 0 ? length / 6 : parseInt(length / 6) + 1)
                $("#zongye").text(yeshu);
                var num = dangqian * 6 >= length ? length - (dangqian * 6 - 6) : dangqian * 6
                for (var i = dangqian * 6 - 6; i < dangqian * 6 - 6 + num; i++) {
                    var r = data[i];
                    var r_id = r.id;
                    var rName = r.resource_name;
                    var type = r.type;
                    var uploadDate = r.upload_date;
                    var fileUrl = r.file_url;
                    //控件赋值
                    var ii = i % 6;
                    $ziyuanID.eq(ii).text(r_id);
                    $rNameUrl.eq(ii).attr("href", "http://" + fileUrl);
                    $rNameUrl.eq(ii).attr("style", "color:rgb(173,90,204);");
                    $rNameUrl.eq(ii).attr("download", r_id + "_" + rName);
                    $rNameUrl.eq(ii).text(rName);
                    $type.eq(ii).text(type);
                    $uploadDate.eq(ii).text(uploadDate);
                    $quxiao.eq(ii).attr("hidden", false);
                    $quxiao.eq(ii).val("取消收藏");
                }
                //重新搜索加载
                // for (let i = 0; i < data.length; i++) {
                //     var resource = data[i];
                //     console.log("表2：" + resource.toString());
                //     var l_id = "fu" + i;
                //     var l_id2 = "sfu" + i;
                //     var l_id3 = "ssfu" + i;
                //     var resource_id = resource.id + "";
                //     var resourceName = resource.resource_name;
                //     var type = resource.type;
                //     var uploadDate = resource.upload_date;
                //     var file_url = resource.file_url;
                //     console.log(file_url);
                //     var td = "<tr id='"+ l_id +"'><td id='"+ l_id2 +"'><input id='"+ l_id3 +"' type='text' value= '"+resource_id +"' hidden><a href='http://"+file_url+"' style='color: rgb(173,90,204)' download='"+resource_id + "_" + resourceName +"'> " + resourceName + "</a></td><td>" + type + "</td><td>" + uploadDate + "</td><td><input type='button' onclick='subCollect(this)' value='取消收藏'></td></tr>"
                //     console.log(td);
                //     $("#t2").append(td);
                // }
            },
            error: function () {
                console.log("错误~")
            }
        });
    }

    //查询用户上传列表
    function findUserName() {
        var userName = "<%= user.getUsername()%>";
        console.log(userName);
        $.ajax({
            url: "resource",
            data: {
                "methodName": "findByNameOrUserName",
                "userName": userName
            },
            type: "get",
            dataType: "json",
            success: function (data) {
                var $rName = $(".rName");
                var $type1 = $(".type1");
                var $upload_date = $(".upload_date");

                for (var i = 0; i < 6; i++) {
                    $rName.eq(i).attr("href", "");
                    $rName.eq(i).attr("style", "");
                    $rName.eq(i).attr("download", "");
                    $rName.eq(i).text("");
                    $type1.eq(i).text("-");
                    $upload_date.eq(i).text("-");
                }

                var ye1 = $("#dangqianye1").text();
                var dangqian1 = parseInt(ye1);
                var length1 = data.length;
                var yeshu1 = (length1 % 6 == 0 ? length1 / 6 : parseInt(length1 / 6) + 1)
                $("#zongye1").text(yeshu1);
                var num1 = dangqian1 * 6 >= length1 ? length1 - (dangqian1 * 6 - 6) : dangqian1 * 6
                for (var i = dangqian1 * 6 - 6; i < dangqian1 * 6 - 6 + num1; i++) {
                    var r = data[i];
                    var resourceId = r.resource_id;
                    var rName = r.resource_name;
                    var type = r.type;
                    var uploadDate = r.upload_date;
                    var fileUrl = r.file_url;
                    //控件赋值
                    var ii = i % 6;
                    $rName.eq(ii).attr("href", "http://" + fileUrl);
                    $rName.eq(ii).attr("style", "color:rgb(173,90,204);");
                    $rName.eq(ii).attr("download", resourceId + "_" + rName);
                    $rName.eq(ii).text(rName);
                    $type1.eq(ii).text(type);
                    $upload_date.eq(ii).text(uploadDate.substring(0, 11));
                }
                // for (let i = 0; i < data.length; i++) {
                //     var resource = data[i];
                //     console.log("表1：" + resource.toString());
                //     var resourceName = resource.resource_name;
                //     var type = resource.type;
                //     var uploadDate = resource.upload_date;
                //     var file_url = resource.file_url;
                //     console.log(file_url);
                //     var td = "<tr><td><a href='http://"+file_url+"' style='color: rgb(173,90,204)'> " + resourceName + "</a></td><td>" + type + "</td><td>" + uploadDate + "</td></tr>"
                //     console.log(td);
                //     $("#t1").append(td);
                // }
            },
            error: function () {
                console.log("错误~")
            }
        });
    }

</script>
</html>
